package com.tyk.bookstore.back.payment.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.excel.ExcelManager;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.payment.manager.excel.constant.ExcelConstant;
import com.tyk.bookstore.back.payment.manager.excel.model.PaymentRecordExcelVo;
import com.tyk.bookstore.back.payment.model.query.PaymentRecordAddQuery;
import com.tyk.bookstore.back.payment.model.query.PaymentRecordPageQuery;
import com.tyk.bookstore.back.payment.model.query.PaymentRecordUpdateQuery;
import com.tyk.bookstore.back.payment.model.vo.PaymentRecordPageVo;
import com.tyk.bookstore.back.payment.model.vo.PaymentRecordVo;
import com.tyk.bookstore.back.payment.service.PaymentRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author tyk
 * @description 支付记录
 */
@Tag(name = "支付管理/支付记录管理", description = "支付记录管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/payment/payment-record")
public class PaymentRecordController {

    private final PaymentRecordService paymentRecordService;
    private final ExcelManager<PaymentRecordExcelVo> excelManager;

    @Operation(summary = "查询支付记录详情", description = "查询支付记录详情")
    @SaCheckPermission("payment:payment-record:get")
    @GetMapping("{id}")
    public R<PaymentRecordVo> getById(
            @Schema(description = "支付记录ID")
            @NotNull @Min(value = 1)
            @PathVariable Long id
    ) {
        return R.ok(paymentRecordService.getById(id));
    }

    @Operation(summary = "分页查询支付记录", description = "分页查询支付记录")
    @SaCheckPermission("payment:payment-record:page")
    @GetMapping("page")
    public R<PaymentRecordPageVo> getPage(
            @Schema(description = "支付记录分页查询参数")
            @ParameterObject
            @Validated PaymentRecordPageQuery query
    ) {
        return R.ok(paymentRecordService.getPage(query));
    }

    @Operation(summary = "添加支付记录", description = "添加支付记录")
    @SaCheckPermission("payment:payment-record:add")
    @PostMapping
    public R<Object> add(
            @Validated @RequestBody PaymentRecordAddQuery query
    ) {
        paymentRecordService.add(query);
        return R.ok();
    }

    @Operation(summary = "更新支付记录", description = "更新支付记录")
    @SaCheckPermission("payment:payment-record:update")
    @PutMapping
    public R<Object> updateById(
            @Validated @RequestBody PaymentRecordUpdateQuery query
    ) {
        paymentRecordService.updateById(query);
        return R.ok();
    }

    @Operation(summary = "删除支付记录", description = "删除支付记录")
    @SaCheckPermission("payment:payment-record:delete")
    @DeleteMapping("{id}")
    public R<Object> deleteById(
            @Schema(description = "支付记录ID")
            @Min(value = 1) @NotNull
            @PathVariable Long id
    ) {
        paymentRecordService.deleteById(id);
        return R.ok();
    }

    @Operation(summary = "批量删除支付记录", description = "批量删除支付记录")
    @SaCheckPermission("payment:payment-record:delete")
    @DeleteMapping
    public R<Object> deleteBatchByIdSet(
            @Schema(description = "支付记录ID列表")
            @NotEmpty @NotNull Set<Long> idList
    ) {
        paymentRecordService.deleteBatchByIdSet(idList);
        return R.ok();
    }

    @Operation(summary = "导出Excel", description = "导出Excel")
    @SaCheckPermission("payment:payment-record:excel:download")
    @GetMapping("excel")
    public void downloadExcel(PaymentRecordPageQuery query, HttpServletResponse resp) {
        excelManager.write(
                paymentRecordService.getPaymentRecordExcelVoList(query),
                ExcelConstant.PAYMENT_RECORD_FILE_NAME,
                ExcelConstant.PAYMENT_RECORD_SHEET,
                resp
        );
    }
}

